package com.five.calendar.database.observer;

import com.five.calendar.common.observer.Observable;
import com.five.calendar.database.DataProvider;
import com.five.calendar.database.bean.TargetBean;
import com.five.calendar.database.bean.TargetDayBean;

import java.util.List;

/**
 * Created by zhuxiaoxinsx on 17-9-5.
 */

public class TargetDayObservable extends Observable<TargetDayBean, Integer, List<TargetDayBean>> {
    private static final int FLAG_SAVE_TARGET_DAY = 1;

    private static final int FLAG_DELETE_TARGET_DAY = 2;

    private static final int FLAG_QUERY_TARGET_DAY = 3;

    private static final int FLAG_UPDATE_TARGET_DAY = 4;

    private static final int FLAG_QUERY_TARGET_DAY_BY_TARGETID = 5;

    private int mFlag;

    private long mTargetId;

    private String mTime;

    private TargetDayBean mTargetDayBean;

    private List<TargetDayBean> mList;

    public TargetDayObservable(int mFlag){
        this.mFlag = mFlag;
    }

    public TargetDayObservable(int mFlag, TargetDayBean mTargetDayBean){
        this.mFlag = mFlag;
        this.mTargetDayBean = mTargetDayBean;
    }

    private TargetDayObservable(int mFlag, List<TargetDayBean> mList){
        this.mFlag = mFlag;
        this.mList = mList;
    }

    private TargetDayObservable(int mFlag, long mTargetId, String mTime){
        this.mFlag = mFlag;
        this.mTargetId = mTargetId;
        this.mTime = mTime;
    }

    private TargetDayObservable(int mFlag, long mTargetId){
        this.mFlag = mFlag;
        this.mTargetId = mTargetId;
    }

    public static TargetDayObservable queryTarget() {
        return new TargetDayObservable(FLAG_QUERY_TARGET_DAY);
    }

    public static TargetDayObservable queryTargetByTargetId(long mTargetId){
        return new TargetDayObservable(FLAG_QUERY_TARGET_DAY_BY_TARGETID, mTargetId);
    }

    public static TargetDayObservable deleteTarget(List<TargetDayBean> mList) {
        return new TargetDayObservable(FLAG_DELETE_TARGET_DAY, mList);
    }

    public static TargetDayObservable updateTarget(TargetDayBean mTargetBean) {
        return new TargetDayObservable(FLAG_UPDATE_TARGET_DAY, mTargetBean);
    }

    public static TargetDayObservable saveTarget(long mTargetId, String mTime) {
        return new TargetDayObservable(FLAG_SAVE_TARGET_DAY, mTargetId, mTime);
    }
    @Override
    public void run() {
        switch (mFlag) {
            case FLAG_SAVE_TARGET_DAY:
                TargetDayBean targetDayBean = DataProvider.getInstance().insertTargetDayBean(mTargetId, mTime);
                if (null != targetDayBean) {
                    handleSuccess(targetDayBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
            case FLAG_DELETE_TARGET_DAY:
                if (DataProvider.getInstance().deleteTargetDayBean(mList)) {
                    for (TargetDayBean bean : mList) {
                        handleSuccess(bean);
                    }
                } else {
                    handleFail(-1);
                }
                handleComplete(mList);
                break;
            case FLAG_QUERY_TARGET_DAY:
                handleComplete(DataProvider.getInstance().queryTargetDayBean());
                break;
            case FLAG_QUERY_TARGET_DAY_BY_TARGETID:
                handleComplete(DataProvider.getInstance().queryTargetDayBeanByTargetId(mTargetId));
                break;
            case FLAG_UPDATE_TARGET_DAY:
                if (DataProvider.getInstance().updateTargetDayBean(mTargetDayBean)) {
                    handleSuccess(mTargetDayBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
        }
    }
}
